package site.shenjie.common.model;

import lombok.Data;

/**
 * @author ShenJie
 * 2022/2/15 16:48
 */
@Data
public class ResponseModel {
    /**
     * 响应码
     */
    private Integer code;
    /**
     * 提示消息
     */
    private String message;
    /**
     * 响应数据
     */
    private Object data;

    /**
     * 响应：成功
     */
    public static ResponseModel getSuccess(String message, Object data) {
        return getResponModel(1, message, data);
    }

    /**
     * 响应：警告
     */
    public static ResponseModel getWarning(String message, Object data) {
        return getResponModel(2, message, data);
    }

    /**
     * 响应：错误
     */
    public static ResponseModel getError(String message, Object data) {
        return getResponModel(3, message, data);
    }

    private static ResponseModel getResponModel(Integer code, String message, Object data) {
        ResponseModel responseModel = new ResponseModel();

        responseModel.setCode(code);

        switch (code) {
            case 1 -> responseModel.setMessage(message == null || message.length() == 0 ? "success" : message);
            case 2 -> responseModel.setMessage(message == null || message.length() == 0 ? "warning" : message);
            case 3 -> responseModel.setMessage(message == null || message.length() == 0 ? "error" : message);
        }

        responseModel.setData(data);

        return responseModel;
    }
}
